package com.wanpinghui.supplier.user;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.adapter.BusinessTypeAdapter;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.user.UserInfoService;
import com.wanpinghui.wphlibrary.model.user.bean.BusinessTypeBean;
import com.wanpinghui.wphlibrary.model.user.bean.GetBusinessResult;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.utils.TextUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit.callback.YRequestCallback;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/11/22
 */
@ContentView(R.layout.layout_activity_business_type)
public class BusinessTypeActivity extends BaseActivity {

    @ViewInject(R.id.super_recycle_view_now)
    private RecyclerView super_recycle_view_now;

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser mLayoutTitle;

    @ViewInject(R.id.btn_submit)
    private ThrottleButton btn_submit;

    private BusinessTypeAdapter adapter;
    private List<BusinessTypeBean> datas;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        if (getIntent() != null) {
            String dataStr = getIntent().getStringExtra("data");
            if (TextUtil.isNotEmpty(dataStr)) {
                datas = new Gson().fromJson(dataStr, new TypeToken<List<BusinessTypeBean>>() {
                }.getType());
            }
        }
        adapter = new BusinessTypeAdapter(this);
        super_recycle_view_now.setLayoutManager(new LinearLayoutManager(BusinessTypeActivity.this));
        super_recycle_view_now.setAdapter(adapter);

        List<BusinessTypeBean> businessTypeBeanList = new ArrayList<>();
        businessTypeBeanList.add(new BusinessTypeBean("install", "安装"));
        businessTypeBeanList.add(new BusinessTypeBean("repair", "维修"));
        businessTypeBeanList.add(new BusinessTypeBean("rent", "租赁"));
        businessTypeBeanList.add(new BusinessTypeBean("purchase", "采购"));
        adapter.setDatas(businessTypeBeanList);

        WPHSdk.getService(UserInfoService.class).getBusinissData(new YRequestCallback<GetBusinessResult.TagBean>() {
            @Override
            public void onSuccess(GetBusinessResult.TagBean var1) {
                List<BusinessTypeBean> businessTypeBeanList = new ArrayList<>();
                businessTypeBeanList.add(new BusinessTypeBean("install", "安装", var1.isInstall()));
                businessTypeBeanList.add(new BusinessTypeBean("repair", "维修", var1.isRepair()));
                businessTypeBeanList.add(new BusinessTypeBean("rent", "租赁", var1.isRent()));
                businessTypeBeanList.add(new BusinessTypeBean("purchase", "采购", var1.isPurchase()));
                adapter.setDatas(businessTypeBeanList);
//                adapter.setItemlistener(new BusinessTypeAdapter.setOnItemlistener() {
//                    @Override
//                    public void onClick(BusinessTypeBean bean) {
//
//                    }
//                });
            }

            @Override
            public void onFailed(int var1, String message) {

            }

            @Override
            public void onException(Throwable var1) {

            }
        });
    }

    @Override
    public void bindEvents() {
        mLayoutTitle.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_submit.setOnThrottleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WPHSdk.getService(UserInfoService.class)
                        .businessScope(adapter.getSelectItem(), new YRequestCallback<Object>() {
                            @Override
                            public void onSuccess(Object var1) {
                                Intent intent = new Intent();
                                intent.putExtra("data", new Gson().toJson(adapter.getSelectItem()));
                                setResult(0, intent);
                                finish();
                            }

                            @Override
                            public void onFailed(int var1, String message) {
                                showWarningText(message);
                            }

                            @Override
                            public void onException(Throwable var1) {

                            }
                        });
            }
        });

    }

    @Override
    public void initData() {

    }
}
